package com.ruoyi.pcwl.controller;

import java.util.List;
import java.util.Map;

import com.ruoyi.pcwl.domain.MediaFiles;
import com.ruoyi.pcwl.domain.PcwlShopDetails;
import com.ruoyi.pcwl.domain.dto.PcwlShopDetailsDto;
import com.ruoyi.pcwl.domain.query.PcwlShopDetailsQuery;
import com.ruoyi.pcwl.domain.vo.Result;
import com.ruoyi.pcwl.domain.enums.MediaTypeEnum;
import com.ruoyi.pcwl.minio.MinioUtil;
import com.ruoyi.pcwl.service.IMediaFilesService;
import com.ruoyi.pcwl.service.PcwlShopDetailsService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 商铺详情信息 Controller
 */

@Api(tags = "商铺详情信息控制层")
@RestController("pcwlShopDetailsController")
@RequestMapping("/pcwlShopDetails")
public class PcwlShopDetailsController extends BaseController{

	@Resource
	private PcwlShopDetailsService pcwlShopDetailsService;

@Value("${minio.bucketName}")
	private String bucketName;

	@Autowired
	IMediaFilesService mediaFilesService;

	/**
	 * 新增商铺环境信息
	 *
	 */
	@ApiOperation("新增商铺详情信息")
	@PostMapping("/addShopDetails")
	public Result addShopDetails(PcwlShopDetailsDto pcwlShopDetailsDto) {
		MultipartFile[] files = pcwlShopDetailsDto.getFiles();

		for (MultipartFile file : files) {
			//上传文件到minio
			Map<String, Object> map  = null;
			try {
				map = MinioUtil.uploadFile(file, MediaTypeEnum.PCWLSHOPDETAILS);
			} catch (Exception e) {
				throw new RuntimeException("上传文件失败");
			}
			//保存商铺详情信息
			PcwlShopDetails pcwlShopDetails = new PcwlShopDetails();
			BeanUtils.copyProperties(pcwlShopDetailsDto,pcwlShopDetails);
			pcwlShopDetails.setUrl(map.get("path").toString());
			pcwlShopDetails.setMediaFileId(Long.parseLong(map.get("mediaFileId").toString()));
			pcwlShopDetailsService.add(pcwlShopDetails);
		}

		return Result.ok(null);
	}

	/**
	 * 根据条件分页查询
	 */
	@ApiOperation("根据条件分页查询")
	@GetMapping("/loadDataList")
	public Result loadDataList(PcwlShopDetailsQuery query){
		return Result.ok(pcwlShopDetailsService.selectPage(query));
	}

	/**
	 * 新增
	 */
	@ApiOperation("新增")
	@PostMapping("/add")
	public Result add(@RequestBody PcwlShopDetails bean) {
		pcwlShopDetailsService.add(bean);
		return Result.ok(null);
	}

	/**
	 * 批量新增
	 */
	@ApiOperation("批量新增")
	@PostMapping("/addBatch")
	public Result addBatch(@RequestBody List<PcwlShopDetails> listBean) {
		pcwlShopDetailsService.addBatch(listBean);
		return Result.ok(null);
	}

	/**
	 * 批量新增/修改
	 */
	@ApiOperation("批量新增/修改")
	@PostMapping("/addOrUpdateBatch")
	public Result addOrUpdateBatch(@RequestBody List<PcwlShopDetails> listBean) {
		pcwlShopDetailsService.addBatch(listBean);
		return Result.ok(null);
	}

	/**
	 * 根据Id查询对象
	 */
	@ApiOperation("根据Id查询对象")
	@GetMapping("/getPcwlShopDetailsById/{id}")
	public Result getPcwlShopDetailsById(@PathVariable Long id) {
		return Result.ok(pcwlShopDetailsService.getPcwlShopDetailsById(id));
	}

	/**
	 * 根据Id修改对象
	 */
	@ApiOperation("根据Id修改对象")
	@PutMapping("/updatePcwlShopDetailsById/{id}")
	public Result updatePcwlShopDetailsById(@RequestBody PcwlShopDetails bean,@PathVariable Long id) {
		pcwlShopDetailsService.updatePcwlShopDetailsById(bean);
		return Result.ok(null);
	}

	/**
	 * 根据Id删除
	 */

	@ApiOperation("根据Id删除")
	@DeleteMapping("/deletePcwlShopDetailsById/{id}")
	public Result deletePcwlShopDetailsById(@PathVariable Long id) {
		pcwlShopDetailsService.deletePcwlShopDetailsById(id);
		return Result.ok(null);
	}
}
